import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gongge/utils/AudioUtils.dart';
import '../bean/GemBean.dart';
import '../generated/l10n.dart';
import '../main.dart';
import '../utils/CommUtils.dart';

/**
 * 旅游模式游戏结束带分数图片
 */
class TravelGameEnd extends Dialog {
  int currentScore = 0; //当前分数
  List<GemBean>? gemList;
  CallBack? callback; //点击新游戏回调
  bool isgem; //true是否为宝石
  int grade;
  bool istheme;
  int themeCont = 1;
  TravelGameEnd({
    required this.currentScore,
    required this.gemList,
    required this.callback,
    required this.isgem,
    required this.grade,
    required this.istheme,
    required this.themeCont,
  });
  @override
  Widget build(BuildContext context) {
    double size = CommUtils.getWidth(context);
    double hight = CommUtils.getHeight(context);
    return Material(
      type: MaterialType.transparency,
      child: Container(
        width: CommUtils.getISWidth(size, hight) ? hight * 0.7 : size,
        margin: EdgeInsets.only(top: hight * 0.1),
        child: Stack(
          children: [
            getBg(context, size, hight),
            //头部图片
            Stack(
              children: [
                Image.asset('assets/images/game_end_top2.png',
                    //  height: ScreenUtil().setWidth(126),
                    width: double.infinity,
                    fit: BoxFit.fitWidth),
                Align(
                  alignment: Alignment.topCenter,
                  child: Container(
                    height: hight * 0.08,
                    margin: EdgeInsets.only(top: hight * 0.08),
                    child: Center(
                      child: CommUtils.getStroke2(getTopText(context), 25,
                          Color(0xffffde50), Color(0Xff521784), 5, 1),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  String getTopText(BuildContext context) {
    if (themeCont >= 36) {
      AudioUtils.getInstance()?.play2("travel_all_ccomplete.mp3");
    } else {
      if (istheme) {
        //完成本主题
        AudioUtils.getInstance()?.play2("travel_get_medal.mp3");
      } else {
        //完成本等级
        AudioUtils.getInstance()?.play2("travel_one_ccomplete.mp3");
      }
    }

    if (isgem) {
      return S.of(context).finish_gem;
    } else {
      return S.of(context).ongratulations_;
    }
  }

  Widget getBg(BuildContext context, double size, double hight) {
    return Column(
      children: [
        SizedBox(height: hight * 0.1),
        Container(
            width: double.infinity,
            margin: EdgeInsets.only(
                left: ScreenUtil().setWidth(15),
                right: ScreenUtil().setWidth(15)),
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage('assets/images/bg_seting.png'),
                  fit: BoxFit.contain),
            ),
            child: Padding(
              padding: EdgeInsets.only(
                  //不要设左右边距，否则web宽度变化，中间的框框会跟着变化
                  top: hight * 0.09,
                  bottom: hight * 0.08),
              child: Column(
                children: [
                  SizedBox(
                    height: hight * 0.05,
                  ),
                  Container(
                    width: CommUtils.getISWidth(size, hight)
                        ? hight * 0.42
                        : size * 0.65,
                    height: hight * 0.4,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                          image: AssetImage('assets/images/middle.png'),
                          fit: BoxFit.fill),
                    ),
                    child: Padding(
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(15),
                            right: ScreenUtil().setWidth(15)),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            AutoSizeText(
                              S.of(context).do_well,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(13),
                                // fontWeight: FontWeight.bold,
                                color: Colors.white,
                              ),
                              maxLines: 1,
                              textAlign: TextAlign.center,
                              overflow: TextOverflow.ellipsis,
                            ),
                            SizedBox(
                              height: hight * 0.06,
                            ),
                            AutoSizeText(
                              S.of(context).hit_the_target,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(19.5),
                                fontWeight: FontWeight.bold,
                                color: Colors.white,
                              ),
                              maxLines: 1,
                              textAlign: TextAlign.center,
                              overflow: TextOverflow.ellipsis,
                            ),
                            isgem
                                ? Container(
                                    width: double.infinity,
                                    child: Wrap(
                                      children: [
                                        GridView.builder(
                                            itemCount: gemList!.length,
                                            shrinkWrap: true,
                                            padding: EdgeInsets.only(top: 0),
                                            gridDelegate:
                                                SliverGridDelegateWithFixedCrossAxisCount(
                                              crossAxisCount: gemList!.length,
                                              //  mainAxisSpacing: ScreenUtil().setHeight(10), //每行间距
                                              // crossAxisSpacing: ScreenUtil().setWidth(10), //交叉轴每行间距
                                            ),
                                            itemBuilder: (context, index) {
                                              return Container(
                                                  height: ScreenUtil()
                                                      .setHeight(23),
                                                  child: Row(
                                                    //GridView.builder居中
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      Image.asset(
                                                          gemList![index].img,
                                                          width: ScreenUtil()
                                                              .setHeight(23),
                                                          height: ScreenUtil()
                                                              .setHeight(23),
                                                          fit: BoxFit.contain),
                                                      SizedBox(
                                                        width: 2,
                                                      ),
                                                      AutoSizeText(
                                                        '${gemList![index].gemCount}',
                                                        style: TextStyle(
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: ScreenUtil()
                                                              .setSp(13),
                                                          color:
                                                              Color(0xffffffff),
                                                          height: 0.7,
                                                        ),
                                                        maxLines: 1,
                                                        textAlign:
                                                            TextAlign.center,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                      ),
                                                    ],
                                                  ));
                                            })
                                      ],
                                    ),
                                  )
                                : Column(
                                    children: [
                                      SizedBox(
                                        height: ScreenUtil()
                                            .setHeight(hight * 0.06),
                                      ),
                                      AutoSizeText(
                                        S
                                            .of(context)
                                            .fraction_('$currentScore'),
                                        style: TextStyle(
                                          fontSize: ScreenUtil().setSp(20),
                                          fontWeight: FontWeight.bold,
                                          color: Color(0XffFEAE00),
                                          height: 0.7,
                                        ),
                                        maxLines: 1,
                                        textAlign: TextAlign.center,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ],
                                  ),
                            SizedBox(
                              height: ScreenUtil().setHeight(size * 0.03),
                            ),
                          ],
                        )),

                    //
                  ),
                  SizedBox(
                    height: ScreenUtil().setHeight(16),
                  ),

                  //底部
                  Container(
                   width: CommUtils.getISWidth(size, hight)
                        ? hight * 0.38
                        : size * 0.55,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        GestureDetector(
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.pop(context);
                          },
                          child: Container(
                            height: ScreenUtil().setHeight(36.5),
                            child: Image.asset('assets/images/button_back.png',
                                fit: BoxFit.fill),
                          ),
                        ),
                        GestureDetector(
                          onTap: () {
                            if (callback != null) {
                              Navigator.pop(context);
                              callback!(grade);
                            }
                          },
                          child: Container(
                              height: ScreenUtil().setHeight(36.5),
                              width: ScreenUtil().setHeight(100),
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image:
                                      AssetImage('assets/images/new_game.png'),
                                  fit: BoxFit.fill,
                                ),
                              ),
                              alignment: Alignment.center,
                              child: Padding(
                                  padding: EdgeInsets.only(
                                      top: 5, bottom: 7, right: 5, left: 5),
                                  child: CommUtils.getTextGame(
                                    context,
                                    15,
                                    istheme
                                        ? S.current.new_journey
                                        : S.current.next_level,
                                    Color(0xffe7f9c6),
                                    Color(0Xff5afddd),
                                  ))),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            )),
      ],
    );
  }
}
